﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Colorful.Furniture.Enum;
using Colorful.Furniture.ErpCustomerManagement;
using Colorful.Furniture.ErpOrderManagement;
using Colorful.Furniture.Extensions;
using Colorful.Furniture.PermissionManagement;
using Colorful.Furniture.Wrappers;
using FreeSql;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;

namespace Colorful.Furniture.StatisticsManagement
{
    /// <summary>
    /// 工作台统计信息服务
    /// </summary>
    [Authorize]
    public class WorkbenchAppService : ApplicationService
    {

        //private readonly IBaseRepository<StatisticsOrderAmount> _orderAmountRepository;
        private readonly IFreeSql _freeSql;
        private readonly IObjectMapper _objectMapper;
        private readonly IBaseRepository<SysManager> _sysManagerRepository;
        private readonly ICurrentUser _currentUser;

        public WorkbenchAppService(
            //IBaseRepository<StatisticsOrderAmount> orderAmountRepository,
            IFreeSql freeSql,
            IObjectMapper objectMapper,
            IBaseRepository<SysManager> sysManagerRepository,
            ICurrentUser currentUser
        )
        {
            //_orderAmountRepository = orderAmountRepository;
            _freeSql = freeSql;
            _objectMapper = objectMapper;
            _sysManagerRepository = sysManagerRepository;
            _currentUser = currentUser;
        }


        /// <summary>
        ///  获取工作台金额统计
        /// </summary>
        /// <returns></returns>
        public async Task<WorkbenchOutputDto> GetWorkbenchAsync()
        {
            var managerInfo = await _sysManagerRepository.Where(x => x.Id == _currentUser.Id).ToOneAsync();

            if (managerInfo == null)
                throw new UserFriendlyException("登录信息错误！");

            //if (managerInfo.CustomerId != null)
            //{
            //    // 本月订单量
            //    _freeSql.Select<CustomerOrder>()
            //        .Where((o) => o.Status == EnumOrderStatus.充值成功 && o.CustomerId == managerInfo.CustomerId && o.IsTestOrder == 0 && o.IsDeleted == false && o.AddTime > DateTime.Now.AddDays(1 - DateTime.Now.Day).Date)
            //        .Count(out var orderNumberCount);

            //    // 本月订单金额   
            //    var saleMonthAmount = _freeSql.Select<CustomerOrder>()
            //        .Where((o) =>
            //            o.Status == EnumOrderStatus.充值成功 && o.CustomerId == managerInfo.CustomerId && o.IsTestOrder == 0 && o.IsDeleted == false &&
            //            o.AddTime > DateTime.Now.AddDays(1 - DateTime.Now.Day).Date);

            //    var sumSaleMonthAmount = await saleMonthAmount
            //        .ToAggregateAsync((o) => new
            //        {
            //            Amount = o.Sum(o.Key.TotalPrice)
            //        });

            //    // 账户余额 和 授信金额
            //    var customerInfo = await _freeSql.Select<Customer>().Where(x => x.Id == managerInfo.CustomerId).ToOneAsync();

            //    return new WorkbenchOutputDto
            //    {
            //        OrderNumber = orderNumberCount.ToInt() ,
            //        OrderAmount = sumSaleMonthAmount?.Amount ?? 0,
            //        CustomerAmount = customerInfo?.Price ?? 0,
            //        CustomerCreditPrice = customerInfo?.CreditPriceInitial ?? 0,
            //        IsManager = 0
            //    };

            //}
            //else
            //{
            //    // 本月订单量
            //    _freeSql.Select<CustomerOrder>()
            //        .Where((o) => o.Status == EnumOrderStatus.充值成功 && o.IsDeleted == false && o.IsTestOrder == 0 && o.AddTime > DateTime.Now.AddDays(1 - DateTime.Now.Day).Date)
            //        .Count(out var orderNumberCount);

            //    // 本月订单金额   
            //    var saleMonthAmount = _freeSql.Select<CustomerOrder>()
            //        .Where((o) =>
            //            o.Status == EnumOrderStatus.充值成功 && o.IsDeleted == false && o.IsTestOrder == 0 &&
            //            o.AddTime > DateTime.Now.AddDays(1 - DateTime.Now.Day).Date);
            //    var sumSaleMonthAmount = await saleMonthAmount
            //        .ToAggregateAsync((o) => new
            //        {
            //            Amount = o.Sum(Convert.ToInt32(o.Key.TotalPrice))
            //        });

            //    //// 账户余额 和 授信金额
            //    //var customerInfo = await _freeSql.Select<Customer>().Where(x => x.Id == managerInfo.CustomerId).ToOneAsync();

            //    return new WorkbenchOutputDto
            //    {
            //        OrderNumber = orderNumberCount.ToInt(),
            //        OrderAmount = sumSaleMonthAmount.Amount,
            //        CustomerAmount = 0,
            //        CustomerCreditPrice = 0,
            //        IsManager = 1
            //    };

            //}




            return new WorkbenchOutputDto
            {
                OrderNumber = 0,
                OrderAmount = 0,
                CustomerAmount = 0,
                CustomerCreditPrice = 0,
                IsManager = 1
            };

        }


    }
}
